/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pos.Driving.Task;

import javax.vecmath.Vector3d;
import pos.Driving.Camera.Camera;
import pos.Driving.Path.PosPoint;
import pos.Driving.Path.Segment.SidewayDesc;
import pos.Driving.Train.Train;
import pos.IModeController;
import pos.MessageBox.MessageBox;

/**
 *
 * @author Administrator
 */
public class TaskPlayerFactory {

    public static TaskPlayer getTaskPlayer01_01(IModeController modeController) {
        TaskPlayer taskPlayer = new TaskPlayer(modeController);
        taskPlayer.addTask(new Task("车辆展示", "自动进行") {

            @Override
            void initProcess() {
                Train targetTrain = (Train) this.myController.doEvent("GetTrain", this);
                targetTrain.setTrainInitialPos(1300, 1);
                targetTrain.setSpeed(0);
                targetTrain.getDashboard().setControlMode("ATC自动驾驶");
                targetTrain.getDashboard().setStationName("自动控制:软件学院(下行)");
                targetTrain.getDashboard().setDestRef(450);
                targetTrain.getDashboard().setTimeRef(85);
                targetTrain.setIsReversed(false);
                targetTrain.setATPSpeedLimit(77);
                targetTrain.setCurrentMode(Train.MODE_ATO);
                targetTrain.setCurrentStatue(Train.STATUE_OPERATING);
                this.myController.doEvent("SetCameraStatue", Camera.STATUE_CUSTOMIZE);
            }

            @Override
            int isReachConditions() {
                Train targetTrain = (Train) this.myController.doEvent("GetTrain", this);
                if (playerCounter[0] > 10) {////////////////////////////
                    new pos.CommandMsgbox(
                            new MessageBox(null, null,
                            "警告！ATC设备故障", "请按任务指示手动驾驶",
                            new String[]{"开始手动驾驶"},
                            new String[]{"Cancel"})).execute(null);
                    return 1;                                               //动画结束
                }
                targetTrain.setSpeed((10 - playerCounter[0]) * 3);
                PosPoint p = (PosPoint) myController.doEvent("GetPosPoint", targetTrain.getTrainPos());
                double theta = -playerCounter[0] / 10 * Math.PI;
                if (playerCounter[0] < 5) {
                    this.myController.doEvent("SetCustomizeCamera",
                            new Vector3d[]{
                                new Vector3d(p.pos.x + 200d * Math.cos(theta),
                                p.pos.y + 3,
                                p.pos.z + 100d * Math.sin(theta) * (Math.sin(theta) * 0.5 + 1)), //pos
                                new Vector3d(0, Math.PI / 2 - theta, 0)                             //rot
                            });
                }
                return 0;
            }

            @Override
            void postProcess( int conditionID) {
            }
        });

        taskPlayer.addTask(new Task("抵达终点站", "准确停靠至规定位置") {

            @Override
            void initProcess() {
                Train targetTrain = (Train) this.myController.doEvent("GetTrain", this);
               // targetTrain.setTrainInitialPos(450, 1);
                targetTrain.setSpeed(0);
                targetTrain.getDashboard().setControlMode("ATP手动前进");
                targetTrain.getDashboard().setStationName("停靠软件学院(下行)站台");
                targetTrain.getDashboard().setDestRef(450);
                targetTrain.getDashboard().setTimeRef(85);
                //targetTrain.setIsReversed(false);
                //targetTrain.setATPSpeedLimit(77);
                targetTrain.setCurrentMode(Train.MODE_ATPFORWARD);
                targetTrain.setCurrentStatue(Train.STATUE_OPERATING);
                myController.doEvent("SetCameraStatue", Camera.STATUE_FOLLOWING);
                myController.doEvent("ChangeView@4", null);
            }

            @Override
            int isReachConditions() {
                Train targetTrain = (Train) this.myController.doEvent("GetTrain", this);
                if (targetTrain.getTrainPos().d < 400) {          //冲过站
                    return 2;
                }
                if (targetTrain.getCurrentStatue() == Train.STATUE_REACHSTATION &&
                        targetTrain.getStationNum() == 33) {
                    subPoints.put("ST+33T", targetTrain.getDashboard().getTimeDiff());
                    subPoints.put("ST+33D", targetTrain.getDashboard().getDestDiff());
                    return 1;                                               //正确停站
                }
                return 0;
            }

            @Override
            void postProcess( int conditionID) {
                switch (conditionID) {
                    case 1:
                        this.myController.doEvent("SetSideway", new SidewayDesc(0, "Return", false, true));
                        this.myController.doEvent("SetSideway", new SidewayDesc(1, "Final", true, false));
                        new pos.CommandMsgbox(
                            new MessageBox(null, null,
                            "很好!列车靠站成功", "请出驾驶室操作车门",
                            new String[]{"继续(按ENTER键以乘降)"},
                            new String[]{"Cancel"})).execute(null);
                        break;
                    case 2:
                        new pos.CommandMsgbox(
                         new MessageBox(null, null,
                            "运营事故", "违规跳站",
                            new String[]{"重新执行本次任务","放弃本次任务"},
                            new String[]{"ResetTask", "ChangeToMainMenu"})).execute(null);
                        break;
                }
            }
        });

        taskPlayer.addTask(new Task("开关门操作", "开关车门并回到驾驶室") {

            @Override
            void initProcess() {
                Train targetTrain = (Train) this.myController.doEvent("GetTrain", this);
                targetTrain.getDashboard().setStationName("完成开关门并回到驾驶室");
                targetTrain.getDashboard().setTimeRef(105);
            }

            @Override
            int isReachConditions() {
                Train targetTrain = (Train) this.myController.doEvent("GetTrain", this);
                if (targetTrain.getCurrentStatue() == Train.STATUE_READY) {
                    targetTrain.setCurrentStatue(Train.STATUE_OPERATING);
                    return 1;                                               //完成开关门并回到驾驶室
                }
                if (targetTrain.getCurrentStatue() == Train.STATUE_OPERATING) {
                    new pos.CommandMsgbox(
                            new MessageBox(null, null,
                            "请下车打开车门！", "必须完成车门操作才可继续",
                            new String[]{"继续(按ENTER键以乘降)"},
                            new String[]{"Cancel"})).execute(null);
                    targetTrain.setCurrentStatue(Train.STATUE_REACHSTATION);
                    targetTrain.setPow(0);
                    targetTrain.setSpeed(0);
                    return 0;                                               //完成开关门并回到驾驶室
                }
                return 0;
            }

            @Override
            void postProcess( int conditionID) {
            }
        });

        taskPlayer.addTask(new Task("运行至折返点", "准确停靠至规定位置") {

            @Override
            void initProcess() {
                Train targetTrain = (Train) this.myController.doEvent("GetTrain", this);
                targetTrain.getDashboard().setStationName("运行至折返点");
                targetTrain.getDashboard().setDestRef(30);
                targetTrain.getDashboard().setTimeRef(165);
                new pos.CommandMsgbox(
                            new MessageBox(null, null,
                            "GOOD！道岔已打开", "请驾车至折返线上",
                            new String[]{"继续"},
                            new String[]{"Cancel"})).execute(null);
            }

            @Override
            int isReachConditions() {
                Train targetTrain = (Train) this.myController.doEvent("GetTrain", this);
                if (targetTrain.getTrainPos().d < 11) {          //冲出折返区域
                    return 2;
                }
                if (targetTrain.getTrainPos().d <= 30 &&
                        targetTrain.getSpeed() < 0.01) {
                    subPoints.put("RTT", targetTrain.getDashboard().getTimeDiff());
                    subPoints.put("RTD", targetTrain.getDashboard().getDestDiff()/4);
                    return 1;                                               //正确停止
                }
                return 0;
            }

            @Override
            void postProcess( int conditionID) {
                switch (conditionID) {
                    case 1:
                        this.myController.doEvent("SetSideway", new SidewayDesc(0, "Return", false, false));
                        this.myController.doEvent("SetSideway", new SidewayDesc(1, "Final", false, false));
                        ((Train) this.myController.doEvent("GetTrain", this)).setIsReversed(true);
                        new pos.CommandMsgbox(
                            new MessageBox(null, null,
                            "折返完成", "重新发车前请牢记操作流程",
                            new String[]{"换向并继续"},
                            new String[]{"Cancel"})).execute(null);
                        break;
                    case 2:
                        new pos.CommandMsgbox(
                         new MessageBox(null, null,
                            "事故", "列车冲过线路安全区域",
                            new String[]{"重新执行本次任务","放弃本次任务"},
                            new String[]{"ResetTask", "ChangeToMainMenu"})).execute(null);
                        break;
                }
            }
        });

        taskPlayer.addTask(new Task("重新出发", "准确停靠至规定位置") {

            @Override
            void initProcess() {
                Train targetTrain = (Train) this.myController.doEvent("GetTrain", this);
                targetTrain.getDashboard().setStationName("完成软件学院(上行)到站动作");
                targetTrain.getDashboard().setDestRef(600);
                targetTrain.getDashboard().setTimeRef(240);
            }

            @Override
            int isReachConditions() {
                Train targetTrain = (Train) this.myController.doEvent("GetTrain", this);
                if (targetTrain.getTrainPos().d > 630) {          //冲过站
                    return 2;
                }
                if (targetTrain.getCurrentStatue() == Train.STATUE_READY &&
                        targetTrain.getStationNum() == -1) {
                    targetTrain.setCurrentStatue(Train.STATUE_OPERATING);
                    return 1;                                               //正确停站
                }
                return 0;
            }

            @Override
            void postProcess( int conditionID) {
                switch (conditionID) {
                    case 1:
                        Train targetTrain = (Train) this.myController.doEvent("GetTrain", this);
                        subPoints.put("ST-1T", targetTrain.getDashboard().getTimeDiff());
                        subPoints.put("ST-1D", targetTrain.getDashboard().getDestDiff());
                        break;
                    case 2:
                        new pos.CommandMsgbox(
                         new MessageBox(null, null,
                            "运营事故", "违规跳站",
                            new String[]{"重新执行本次任务","放弃本次任务"},
                            new String[]{"ResetTask", "ChangeToMainMenu"})).execute(null);
                        break;
                }
            }
        });

        taskPlayer.addTask(new Task("驶往远方", "GO") {

            @Override
            void initProcess() {
                Train targetTrain = (Train) this.myController.doEvent("GetTrain", this);
                targetTrain.getDashboard().setStationName("向下一站出发");
                targetTrain.getDashboard().setDestRef(1200);
                targetTrain.getDashboard().setTimeRef(285);
            }

            @Override
            int isReachConditions() {
                Train targetTrain = (Train) this.myController.doEvent("GetTrain", this);
                if (targetTrain.getTrainPos().d > 1200) {          //完成
                    return 1;
                }
                return 0;
            }

            @Override
            void postProcess( int conditionID) {
                switch (conditionID) {
                    case 1:
                        Train targetTrain = (Train) this.myController.doEvent("GetTrain", this);
                        subPoints.put("ST-2T", targetTrain.getDashboard().getTimeDiff());
                        double totalTimeDiff=conditionAbs(subPoints.get("ST+33T"))
                                +conditionAbs(subPoints.get("RTT"))
                                +conditionAbs(subPoints.get("ST-1T"))
                                +conditionAbs(subPoints.get("ST-2T"));
                        
                        double totalDestDiff=Math.abs(subPoints.get("ST+33D"))
                                +Math.abs(subPoints.get("RTD"))
                                +Math.abs(subPoints.get("ST-1D"));
                        long timePts=Math.round(100d*Math.pow(2, -totalTimeDiff/60));
                        long destPts=Math.round(100d*Math.pow(2, -totalDestDiff/15));
                        long finalPts=(timePts+destPts)/2;
                        
                        new pos.CommandMsgbox(
                         new MessageBox(null, null,
                            "任务完成！", "运营质量评价:"+finalPts+"分",
                            new String[]{"准点运行:"+timePts+"分","准确停靠:"+destPts+"分",
                                                "重新执行本次任务","返回主菜单"},
                            new String[]{"FinshTask", "FinshTask","ResetTask", "ChangeToMainMenu"})).execute(null);
                        break;
                }
            }
            private double conditionAbs(double input){
                if (input>0)
                    return input/3d;
                else
                    return -input;
            }
        });
        return taskPlayer;
    }
}
